﻿using Furion.DependencyInjection;
using Furion.LinqBuilder;
using XinGuanERP.Db;
using XinGuanERP.Models.Po.system;
using XinGuanERP.Services.system.IServices;
using XinGuanERP.Utils;

namespace XinGuanERP.Services.system
{
    public class SysActionLogService : ISysActionLogService, ITransient
    {
        private readonly DefaultDbContext _defaultDbContext;

        public SysActionLogService(DefaultDbContext defaultDbContext)
        {
            _defaultDbContext = defaultDbContext;
        }

        public PageResult<SysActionLog> GetSysActionLogByPage(int page, int pageSize, string? userId)
        {
            var res = new List<SysActionLog>();
            var count = 0;
            var sql = _defaultDbContext.SysActionLogs.AsQueryable();
            if (userId.IsNullOrEmpty())
            {
                res = sql.Skip((page - 1) * pageSize).Take(pageSize).ToList();
                count = sql.Count();
            }
            else
            {
                res = sql.Where(t => t.ActionUserId == userId)
                    .Skip((page - 1) * pageSize).Take(pageSize).ToList();
                count = sql.Where(t => t.ActionUserId == userId)
                    .Count();
            }
            return new PageResult<SysActionLog>()
            {
                Items = res,
                Total = count
            };
        }
    }
}
